package com.wuxiaofan.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
	/**缺省日期格式*/
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

	/**缺省日期格式*/
	public static final String DEFAULT_DATE_FORMAT_YM = "yyyyMM";

	/**缺省日期格式*/
	public static final String DEFAULT_DATE_FORMAT_YEAR = "yyyy";

	/**缺省时间格式*/
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	
	/**缺省时间格式-时分秒*/
	public static final String DEFAULT_TIME_FORMAT_HMS = "HHmmss";

	/**缺省记录日期格式-24小时制*/
	public static final String DEFAULT_RECORD_TIME_FORMAT = "yyyyMMddHHmmssSSS";
	
	/**缺省记录日期格式-24小时制*/
	public static final String NORMAL_TIME_FORMAT = "yyyyMMddHHmmss";

	/**缺省记录日期格式-年月日*/
	public static final String DEFAULT_DATE_FORMAT_YMD = "yyyyMMdd";

	/**月日时分*/
	public static final String DEFAULT_DATE_FORMAT_MDHM = "yyyyMMdd";

	/**缺省月格式*/
	public static final String DEFAULT_MONTH = "MONTH";

	/**缺省年格式*/
	public static final String DEFAULT_YEAR = "YEAR";

	/**缺省日格式*/
	public static final String DEFAULT_DATE = "DAY";

	/**缺省小时格式*/
	public static final String DEFAULT_HOUR = "HOUR";

	/**缺省分钟格式*/
	public static final String DEFAULT_MINUTE = "MINUTE";

	/**缺省秒格式*/
	public static final String DEFAULT_SECOND = "SECOND";

	/**缺省长日期格式*/
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH-mm";

	/**缺省长日期格式,精确到秒*/
	public static final String DEFAULT_DATETIME_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";

	/**缺省长日期格式,精确到毫秒*/
	public static final String DEFAULT_DATETIME_FORMAT_SEC_2 = "yyyy-MM-dd HH:mm:ss.ms";
	
	/**
	 * 时间格式 ：yyyy-mm-dd hh24:mi:ss
	 */
	public static final String DATE_FORMAT_YMDHMS = "yyyy-mm-dd hh24:mi:ss";
	
	/**
	 * 时间格式 ：yyyy-mm-dd hh24:mi:ss.ff
	 */
	public static final String DATE_FORMAT_YMDHMS_FF = "yyyy-mm-dd hh24:mi:ss.ff";
	
	/**
	 * 时间格式 ：yyyy-MM-dd HH:mm:ss.SSSSSS
	 */
	public static final String DATE_FORMAT_YMDHMSS = "yyyy-MM-dd HH:mm:ss.SSSSSS";

	/**星期数组*/
	public static final String[] WEEKS = { "星期日", "星期一", "星期二", "星期三", "星期四",
			"星期五", "星期六" };
	public static String DEFAULT_YM_FORMAT = "yyyy-MM";



	public static boolean compareDateStr(String dateStr1,String dateStr2){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		try {
			Date date1 =sdf.parse(dateStr1);
			Date date2 =sdf.parse(dateStr2);
			return date2.after(date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 返回 yyyyMMddHHmmssSSS 格式当前时间字符串
	 * @return
	 */
	public static String getNowDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		return sdf.format(new Date());
	}

	//times yyyyMMddHHmmssSSS
	public static String formateTime(String times) {
		String dateStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		SimpleDateFormat sdf2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC);
		try{
			Date date = sdf.parse(times);
			dateStr = sdf2.format(date);
		}catch (Exception e){
		}
		return dateStr;
	}



	/**
	 * 返回 yyyyMMddHHmmss 格式当前时间字符串
	 * @return
	 */
	public static String getNewStrDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(NORMAL_TIME_FORMAT);
		return sdf.format(new Date());
	}

	public static String getNowDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		return sdf.format(date);
	}

	public static String getNewDate(){
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		// new Date()为获取当前系统时间
		return df.format(new Date());
	}

	public static String getNowDateYYYYMMDD(){
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YMD);
		// new Date()为获取当前系统时间
		return df.format(new Date());
	}


	public static int getNowHour() {
		return new Date().getHours();
	}

	/**
	 * 返回 将yyyyMMddHHmmssSSS 格式时间字符串 转换为 yyyy-MM-dd HH:mm:ss.ms 格式
	 * @return
	 */
	public static String getNowDateStr(String dateStr) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		SimpleDateFormat sdfSec = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC_2);
		try {
			Date date = sdf.parse(dateStr);
			str = sdfSec.format(date);
		} catch (Exception e) {
		}
		return str;
	}
	/**
	 * 返回 将yyyyMMddHHmmssSSS 格式时间字符串 转换为 yyyy-MM-dd HH:mm:ss 格式
	 * @return
	 */
	public static String getDateStr(String dateStr) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		SimpleDateFormat sdfSec = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC);
		try {
			Date date = sdf.parse(dateStr);
			str = sdfSec.format(date);
		} catch (Exception e) {
		}
		return str;
	}

	/**
	 * 将yyyy-MM-dd HH:mm:ss 格式时间字符串 转换为yyyyMMddHHmmssSSS 格式
	 * @param dateStr
	 * @return
	 */
	public static String geDateStr(String dateStr) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		SimpleDateFormat sdfSec = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC);
		try {
			Date date = sdfSec.parse(dateStr);
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}

	/**
	 * 获取yyyy-mm-dd hh-mm-ss
	 * @return
	 */
	public static String getyyyyMMddHHmmss() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC);
		try {
			Date date = new Date();
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}
	/**
	 * 取yyyyMMdd格式的时间字符串
	 * @param date
	 * @return
	 */
	public static String getDateStr(Date date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YMD);
		try {
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}


	/**
	 *
	 * @param dateStr  日期格式 yyyy-mm-dd
	 * @return
	 */
	public static Date getDateByStr(String dateStr){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		try {
			return sdf.parse(dateStr);
		} catch (Exception e) {
		}
		return null;
	}


	public static String getDateStrByHour(int hour) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		try {
			Date date = new Date();
			date.setHours(hour);
			date.setMinutes(0);
			date.setSeconds(0);
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}

	public static String getDateStrByDateAndHour(String dateStr,int hour) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		SimpleDateFormat dateSdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		try {
			Date date = dateSdf.parse(dateStr);
			date.setHours(hour);
			date.setMinutes(0);
			date.setSeconds(0);
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}


	/**
	 * 取yyyyMMddHHmissSSS格式的时间字符串
	 * @param date
	 * @return
	 */
	public static String getTimeStr(Date date) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		try {
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}
	/**
	 *格式化 yyyyMMddHHmissSSS格式的时间
	 * @param dateStr
	 * @return
	 */
	public static Date getTimeByStr(String dateStr) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
		}
		return date;
	}

	public static Date getNowDateByStr(String dateStr){
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
		}
		return date;
	}
	public static String getNowDateStr() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YMD);
		try {
			Date date = new Date();
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}

	public static String getNowDateStryyyymm() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YM);
		try {
			Date date = new Date();
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}

	public static String getNewDateYYYY(){
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YEAR);
		try {
			Date date = new Date();
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}

    /**
     * 获取当前时间几个小时之后的时间
     * @param hourNum
     * @return
     */
    public static Date getAfterHour(int hourNum) {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.HOUR,hourNum);
        return calendar.getTime();
    }

    /**
     * 获取当前时间几分钟之后的时间
     * @param minuteNum
     * @return
     */
	public static Date getAfterMinute(int minuteNum) {
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MINUTE,minuteNum);
		return calendar.getTime();
	}

	public static Date getBeforeMinute(int minuteNum) {
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MINUTE,-minuteNum);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间多少秒之后的时间
	 * @param secNum
	 * @return
	 */
	public static String getAfterSec(String dateStr,int secNum) {
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		try {
			Date date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.SECOND, secNum);
			return sdf.format(calendar.getTime());
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前时间几个小时之后的时间
	 * @param hourNum
	 * @return
	 */
	public static Date getAfterHour(Date oldDate,int hourNum) {
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(oldDate);
		calendar.add(Calendar.HOUR,hourNum);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间几天之后的时间
	 * @param dayNum
	 * @return
	 */
	public static Date getAfterDay(int dayNum) {
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE,dayNum);
		return calendar.getTime();
	}

	/**
	 * 获取指定时间几天之后的时间
	 * @param data
	 * @param dayNum
	 * @return
	 */
	public static Date getAfterDay(Date data,int dayNum) {
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(data);
		calendar.add(Calendar.DATE,dayNum);
		return calendar.getTime();
	}

	/**
	 * 获取指定时间几天之后的时间
	 * @param dayNum
	 * @return
	 */
	public static Date getAfterDay(String dataStr ,int dayNum) {
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_RECORD_TIME_FORMAT);
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.DATE,dayNum);
		}catch (Exception e){
		}
		return calendar.getTime();
	}

	/**
	 * 取当前时间前几天的时间
	 * @return
	 */
	public static Date getBeforeDay(int num) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 是否限制时间大于当前时间
	 * @return
	 */
	public static boolean greaterThanNowTime(String timeStr){
		Date limitTime = DateUtils.getTimeByStr(timeStr);
		if(limitTime==null){
			return true;
		}
		return new Date().after(limitTime);
	}

	public static String getBeforeDayStr(int num) {
		return getDateStr(getBeforeDay(num));
	}

	public static String getDayStr(String dateFormatStr) {
		String str="";
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormatStr);
		try {
			Date date = new Date();
			str = sdf.format(date);
		} catch (Exception e) {
		}
		return str;
	}


	public static List<String> findDates(String startTime, String endTime) throws ParseException {
		//日期工具类准备
		DateFormat format = new SimpleDateFormat("yyyyMMdd");

		//设置开始时间
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(format.parse(startTime));

		//设置结束时间
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(format.parse(endTime));

		//装返回的日期集合容器
		List<String> Datelist = new ArrayList<String>();

		// 每次循环给calBegin日期加一天，直到calBegin.getTime()时间等于dEnd
		while (format.parse(endTime).after(calBegin.getTime())) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			Datelist.add(format.format(calBegin.getTime()));
		}
		return Datelist;
	}

	//获取上一个月的时间
	public static String getMonthDate(int num){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		Calendar calendar = Calendar.getInstance();
		//得到前一月
		calendar.add(Calendar.MONTH, num);
		Date date = calendar.getTime();
		String dateStr = formatter.format(date);
		return dateStr;
	}

	//获取指定时间上一个月的时间YYYYMM
	public static String getBeforeMonthDate(String dataStr,int num){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.MONTH,num);
			Date str=calendar.getTime();
			dateStr=sdf.format(str);
		}catch (Exception e){
		}
		return dateStr;
	}

	//获取指定时间上一个月的时间YYYYMMddhhmmss
	public static String getBeforeMonthDateYYYYMMddhhmmss(String dataStr,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_SEC);
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.MONTH,num);
			Date str=calendar.getTime();
			dateStr=sdf.format(str);
		}catch (Exception e){
		}
		return dateStr;
	}

	//获取指定时间多少个月以前的时间YYYYMMdd
	public static String getBeforeMonthDateYYYYMMdd(String dataStr,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YMD);
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.MONTH,num);
			Date str=calendar.getTime();
			dateStr=sdf.format(str);
		}catch (Exception e){
		}
		return dateStr;
	}

	//获取指定时间多少天以前的时间YYYYMMdd
	public static String getBeforeDayDateYYYYMMdd(String dataStr,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_YMD);
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.DATE,num);
			Date str=calendar.getTime();
			dateStr=sdf.format(str);
		}catch (Exception e){
		}
		return dateStr;
	}

	//获取上一年的时间
	public static String getYearDate(int num){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		Calendar calendar = Calendar.getInstance();
		//得到前一年
		calendar.add(Calendar.YEAR, num);
		Date date = calendar.getTime();
		String dateStr = formatter.format(date);
		return dateStr;
	}

	//获取指定时间上一年的时间
	public static String getBeforeYearDate(String dataStr ,int num) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.YEAR,num);
			Date Str=calendar.getTime();
			dateStr = sdf.format(Str);
		}catch (Exception e){
		}
		return dateStr;
	}


	//获取指定时间上一个月的时间YYYYMMdd
	public static String getDateYYYYMMdd(String dataStr,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		String dateStr="";
		Calendar calendar=Calendar.getInstance();
		try{
			Date date = sdf.parse(dataStr);
			calendar.setTime(date);
			calendar.add(Calendar.MONTH,num);
			Date str=calendar.getTime();
			dateStr=sdf.format(str);
		}catch (Exception e){
		}
		return dateStr;
	}


	/**
	 * 获取长整型毫秒数
	 * @return
	 */
	public static Long getDateGetTime(){
		Date date = new Date();
		return date.getTime();
	}

	/**
	 * 获取指定时间的长整型毫秒数
	 * @return
	 */
	public static Long getNewDateTime(){
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		Date date = null;
		try {
			date = format.parse(getDateYYYYMMdd(getNewDate(),3));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long millis = date.getTime();
		return millis;
	}
}

